<template>
	<!-- 添加任务 -->
	<view class="building-add">
		<view class="add-head">
			<view class="head-left">
				<view class="head-left-1">上传图片</view>
				<view class="head-left-2">图片建议大小不超过2M</view>
			</view>
			<view class="head-right" @click="chooseImage">
				<image class="img" v-if="imgs==''" src="../../../static/icon/jia-.png"></image>
				<image class="img2" v-else :src="imgs"></image>
			</view>
		</view>
		<view class="add-content">
			<view class="add-item">
				<view class="item-left">任务名称<text>*</text></view>
				<view class="item-right"><input placeholder="请输入名称" placeholder-style="color:#999;font-size:26rpx" v-model="title"/></view>
			</view>
			<view class="add-item">
				<view class="item-left">开始时间<text>*</text></view>
				<view class="item-right" @click="dateShow=true">{{ startStart }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">结束时间<text>*</text></view>
				<view class="item-right" @click="dateShow2=true">{{ endStart }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">任务目标类型<text>*</text></view>
				<view class="item-right" @click="Show1=true">{{ targetType_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">任务类型<text>*</text></view>
				<view class="item-right" @click="Show3=true">{{ taskType_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">任务状态<text>*</text></view>
				<view class="item-right" @click="Show2=true">{{ taskStatus_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">是否完成<text>*</text></view>
				<view class="item-right" @click="Show=true">{{ isFinished_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">描述<text>*</text></view>
				<view class="item-right"><textarea placeholder="请输入描述" placeholder-style="color:#999;font-size:26rpx" v-model="description"></textarea></view>
			</view>
		</view>
		<view class="btn">
			<view class="btn-1" @click="handleSubmit">保存</view>
		</view>
		
		<u-toast ref="uToast" />
		<u-select v-model="Show" :list="isFinishedList" @confirm="confirmShow"></u-select>
		<u-select v-model="Show1" :list="targetTypeList" @confirm="confirmShow1"></u-select>
		<u-select v-model="Show2" :list="taskStatusList" @confirm="confirmShow2"></u-select>
		<u-select v-model="Show3" :list="taskTypeList" @confirm="confirmShow3"></u-select>
		<u-picker mode="time" v-model="dateShow" :params="params" @confirm="confirmDateShow"></u-picker>
		<u-picker mode="time" v-model="dateShow2" :params="params" @confirm="confirmDateShow2"></u-picker>
	</view>
</template>

<script>
	export default{
		data() {
			return{
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				dateShow: false,
				dateShow2: false,
				Show:false,
				Show1:false,
				Show2:false,
				Show3:false,
				description:'',
				endStart:'请选择',// 结束时间
				startStart:'请选择',// 开始时间
				imgs:'',//图片
				datumType:'',// 资料类型
				isFinished:false,// 是否完成
				isFinishedList:[],
				isFinished_text:'请选择',
				targetType:'',// 任务目标类型
				targetType_text:'请选择',
				targetTypeList:[],
				// tagetId:'',// 任务主ID
				taskStatus:'',// 任务状态
				taskStatus_text:'请选择',
				taskStatusList:[],
				taskType:'',// 任务类型
				taskType_text:'请选择',
				taskTypeList:[],
				title:'',// 标题
			}
		},
		mounted() {
			this.getDictionaries()
			this.getDictionaries2()
			this.getDictionaries1()
			this.getDictionaries3()
		},
		onShow() {
			
		},
		methods:{
			confirmShow(e) {
				this.isFinished = e[0].value
				this.isFinished_text = e[0].label
			},
			confirmShow1(e) {
				this.targetType = e[0].value
				this.targetType_text = e[0].label
			},
			confirmShow2(e) {
				this.taskStatus = e[0].value
				this.taskStatus_text = e[0].label
			},
			confirmShow3(e) {
				this.taskType = e[0].value
				this.taskType_text = e[0].label
			},
			confirmDateShow(e) {
				this.startStart = e.year + '-' + e.month + '-' + e.day
			},
			confirmDateShow2(e) {
				this.endStart = e.year + '-' + e.month + '-' + e.day
			},
			// 是否完成
			getDictionaries() {
				let params = {
					dictCode:'task_is_finished_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.isFinishedList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 任务目标类型
			getDictionaries1() {
				let params = {
					dictCode:'target_type_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.targetTypeList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 任务状态
			getDictionaries2() {
				let params = {
					dictCode:'task_status_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.taskStatusList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 任务类型
			getDictionaries3() {
				let params = {
					dictCode:'task_type_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.taskTypeList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 上传图片
			chooseImage() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								self.imgs = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 提交 添加
			handleSubmit() {
				if(this.imgs == '') {
					return this.$refs.uToast.show({
						title: '请上传图片'
					})
				}
				if(this.title == '') {
					return this.$refs.uToast.show({
						title: '请输入任务标题'
					})
				}
				if(this.startStart == '') {
					return this.$refs.uToast.show({
						title: '请选择开始时间'
					})
				}
				if(this.endStart == '') {
					return this.$refs.uToast.show({
						title: '请选择结束时间'
					})
				}
				if(this.targetType == '') {
					return this.$refs.uToast.show({
						title: '请选择任务目标类型'
					})
				}
				if(this.taskStatus == '') {
					return this.$refs.uToast.show({
						title: '请选择任务状态'
					})
				}
				if(this.taskType == '') {
					return this.$refs.uToast.show({
						title: '请选择任务类型'
					})
				}
				let params = {
					imgs:this.imgs,
					description:this.description,
					endStart:this.endStart,
					isFinished:this.isFinished,
					startStart:this.startStart,
					targetType:this.targetType,
					taskStatus:this.taskStatus,
					taskType:this.taskType,
					title:this.title
				}
				console.log(params)
				this.$u.api.submitTask(params)
					.then(res => {
						console.log(res)
						if(res.code ==  200) {
							uni.showToast({
								title:'添加成功',
								icon:'success'
							})
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							title:'添加失败',
							icon:'none'
						})
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #cecece;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				.img{
					width: 50%;
					height: 50%;
				}
				.img2{
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			padding-bottom: 110rpx;
			.add-item{
				display: flex;
				// justify-content: space-between;
				align-items: flex-start;
				padding: 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					flex: 2;
					text-align: left;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					flex: 6;
					font-size: 27rpx;
					color: #999;
					text-align: right;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					text{
						display: inline-block;
						margin-left: 8rpx;
					}
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #cecece;
			.btn-1{
				background-color: #007AFF;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
		}
	}
</style>
